<?php

/********************************************************
Reverse geocoding in order to obtain the name and address
*********************************************************/
$lat = $_GET["lat"];
$long = $_GET["lng"];

$query = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $long;
$query = $query . "&sensor=false";

$json = file_get_contents($query);
$arr = json_decode($json, true);
$results = $arr["results"];

$name = $results[0]["address_components"][1]["long_name"];
$address = $results[0]["formatted_address"];

echo "Latitude: " . $lat . "<br/>";
echo "Longitude: " . $long. "<br/>";
echo "Name: " . $name . "<br/>";
echo "Address: " . $address . "<br/>";

/********************************************************
Write necessary information into the txt file
*********************************************************/

$file = "test.txt";

$info = "";
$info .= $lat . "\n";
$info .= $long . "\n";
$info .= $name . "\n";
$info .= $address . "\n";

// Write the contents to the file
file_put_contents($file, $info);

//echo "<br/>Try reading test.txt now";

?>